import {Injectable} from '@angular/core';
import {ConfigProvider} from "../config/config";
import {ActionSheetController, AlertController, LoadingController, ModalController} from "ionic-angular";

@Injectable()
export class UtilsProvider {

    constructor(
        public config: ConfigProvider,
        public alertCtrl: AlertController,
        public actionSheetCtrl: ActionSheetController,
        public loadingCtrl: LoadingController,
        public modalCtrl: ModalController
    ) {

    }

    // 获取当前时间戳
    getTime() {
        return Math.round(new Date().getTime() / 1000);
    }

    getRangeDate( range?: number, type?: string ) {

        const formatDate = ( time: any ) => {
            // 格式化日期，获取今天的日期
            const Dates = new Date( time );
            const year: number = Dates.getFullYear();
            const month: any = ( Dates.getMonth() + 1 ) < 10 ? '0' + ( Dates.getMonth() + 1 ) : ( Dates.getMonth() + 1 );
            const day: any = Dates.getDate() < 10 ? '0' + Dates.getDate() : Dates.getDate();
            return year + '-' + month + '-' + day;
        };

        const now = formatDate( new Date().getTime() ); // 当前时间
        const resultArr: Array<any> = [];
        let changeDate: string;
        if ( range ) {
            if ( type ) {
                if ( type === 'one' ) {
                    changeDate = formatDate( new Date().getTime() + ( 1000 * 3600 * 24 * range ) );
                }
                if ( type === 'more' ) {
                    if ( range < 0 ) {
                        for ( let i = Math.abs( range ); i >= 0; i-- ) {
                            resultArr.push( formatDate( new Date().getTime() + ( -1000 * 3600 * 24 * i ) ) );
                        }
                    } else {
                        for ( let i = 1; i <= range; i++ ) {
                            resultArr.push( formatDate( new Date().getTime() + ( 1000 * 3600 * 24 * i ) ) );
                        }
                    }

                }
            } else {
                changeDate = formatDate( new Date().getTime() + ( 1000 * 3600 * 24 * range ) );
            }
            return changeDate;
        }else{
            return now;
        }

    }

    //序列化表单数据
    serializeParams(data) {
        let paramsStr = '';
        for (let key in data) {
            paramsStr += key + "=" + data[key] + '&'
        }
        return paramsStr.substr(0, (paramsStr.length - 1));
    }

    //图片链接判断
    parseImageUrl(url=null) {
        if (url == undefined || url == null || url == '' || url == 'null') {
            url = this.config.webUrl + "/assets/imgs/no-headimg.png";
            return url;
        }
        if (url.indexOf('https://') != -1 || url.indexOf('http://') != -1) {
            return url;
        } else {
            return this.config.imageUrl + url;
        }
    }

    //判断是否为数组
    isArray(o) {
        return Object.prototype.toString.call(o) == '[object Array]';
    }

    //判断是否在数组中
    inArray(arr, val) {
        if (!this.isArray(arr)) {
            arr = [];
        }
        for (let i = 0; i < arr.length; i++) {
            if (arr[i] == val) return i;
        }
        return -1;
    }

    //确认弹出框
    presentConfirm(message, accept, cancel = function () {}) {
        let alert = this.alertCtrl.create({
            subTitle: message,
            buttons: [
                {
                    text: '确定',
                    handler: () => {
                        accept();
                    }
                },
                {
                    text: '取消',
                    role: 'cancel',
                    handler: () => {
                        cancel()
                    }
                }
            ]
        });
        alert.present();
    }

    //普通弹出框
    presentAlert(message) {
        let alert = this.alertCtrl.create({
            subTitle: message,
            buttons: [
                {
                    text: '确定'
                }
            ]
        });
        alert.present();
    }

    //从下弹出选项
    presentActionSheet(options, callback, title = 'Modify your album') {
        let button = [];
        for (let option of options) {
            button.push({
                text: option.text,
                handler: () => {
                    callback(option)
                }
            });
        }
        button.push({
            text: '取消',
            role: 'cancel',
            handler: () => {
            }
        });
        let actionSheet = this.actionSheetCtrl.create({
            title: title,
            buttons: button
        });

        actionSheet.present();
    }


    /**
     * loading 加载中...
     * */
    loading(options={}){
        let loading = this.loadingCtrl.create(options);
        return {
            show:()=>{
                loading.present();
            },
            close: ()=>{
                loading.dismiss();
            }
        }
    }

    imageSlide(imagesArray, index=0) {
        let profileModal = this.modalCtrl.create('ImageSlidePage', {imagesArray, index});
        profileModal.present();
    }

}
